package com.zlsy.secondtime.threadcoreknowledge.stopthreads;

/**
 * @author zhouliang
 * @date 2020/5/15 18:04
 */
public class RightWayInterrupted {

    public static void main(String[] args) throws InterruptedException {

        Thread threadOne = new Thread(new Runnable() {
            @Override
            public void run() {
                for (; ; ) ;
            }
        });

        threadOne.start();

        threadOne.interrupt();

        //true
        System.out.println("isInterrupted:" + threadOne.isInterrupted());

        /**
         * interrupted（静态方法-当前线程中断状态，标记位清除） 和  isInterrupted（当前线程中断状态，标记位不清除）
         *
         * interrupted-这个方法的对象是当前线程，而不管方法来源于哪个对象。
         * public static boolean interrupted()
         * 由于执行当前方法的线程是main函数，主线程，主线程并没有被中断，所以返回是false
         */

        //false
        System.out.println("isInterrupted:" + threadOne.interrupted());

        //false
        System.out.println("isInterrupted:" + Thread.interrupted());

        // true
        System.out.println("isInterrupted:" + threadOne.isInterrupted());

        threadOne.join();

        //over
        System.out.println("main thread is over.");

    }
}
